window.onload = function() {
	var timer1 = false;//下降定时器
	var timer2 = false;//动态产生字母定时器
	var timer3 = false;//时间计时器
	var time = 0;//计时器时间，单位为秒
	var sum = 0;//掉落的字母总数
	var count = 0;//失败的数量
	var saveCount = 0;//拯救的数量
	


	
	function kaishi() {
		var countContent = document.querySelector('#count');
		if(countContent.style.display == 'block'){
			countContent.style.display = 'none';
			saveCount = 0;
		}
		if(timer1 == false) {
			//字母下降定时器
			timer1 = setInterval(
				function() {
					var div_upper_top = document.querySelector('.div_upper');
					for(var i =0;i<div_upper_top.children.length;i++){
						var content = div_upper_top.children[i];
//						console.log(parseInt(content.getAttribute('speed')));
//						var top_value = parseInt(content.style.top) + 2;
						var top_value = parseInt(content.style.top) + parseInt(content.getAttribute('speed'));				
						content.style.top = top_value + 'px';
						if(top_value > 450){
							div_upper_top.removeChild(div_upper_top.children[i]);
							count++;
//							saveCount = sum - count;
							var correct = document.querySelector('#correct');
							var speed = document.querySelector('#speed');
							correct.innerHTML = '正确率:'+ parseInt((1 - count/sum)*100) + '%';
							speed.innerHTML = '速度:' + parseInt((sum - count) / time) + '字/分钟';
							if(count >  10){
								alert('字母损失严重，已结束本次游戏，开始下一局吧！');
								jieshu();
								var countContent = document.querySelector('#count');
								countContent.style.display = 'block';
								countContent.innerHTML = '字母损失已达10个，已结束本局游戏！';
								correct.innerHTML = '正确率:100%';
							}
						}
					}
				}, 33);
		};
		//动态产生字母定时器
		timer2 = setInterval(function(){
//			var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G',
//				'H', 'I', 'G', 'K', 'L', 'M', 'N',
//				'O', 'P', 'Q', 'R', 'S', 'T', 
//				'U','V', 'W', 'X', 'Y', 'Z'];
//			var index = Math.round(Math.random() * 25);
			var upperChar = Math.round(Math.random()*26+65);
			var div_content = '&#'+ upperChar +';';
			var positionLeft = Math.round(Math.random() * 560);
			var oDiv_upper = document.querySelector('.div_upper');
			var oDiv = document.createElement('div');
			//为upperChar设置属性key。因为属性key是变化的，所以将其与键盘按键的keyCode对比，判断是否正确，是否可以删除
			oDiv.setAttribute('key',upperChar);
			oDiv.setAttribute('speed',(Math.random() * 3) + 1);
			oDiv.className = 'div1';
			oDiv.style.left = positionLeft + 'px';
			oDiv.style.top = '-30px';
//			oDiv.innerHTML = arr[index];
			oDiv.innerHTML = div_content;
			oDiv_upper.appendChild(oDiv);
			sum++;
		},1000);
		
		//时间计时
		timer3 = setInterval(function (){
			time++;
			var seconds = time % 60;
			seconds = seconds<10?'0'+seconds:seconds;
			var minutes = parseInt(time / 60) % 60;
			minutes = minutes < 10 ? '0' + minutes : minutes;
			var hours = parseInt(time / 3600);
			hours = hours<10?'0'+hours:hours;
			var div_time = document.querySelector('#time');
			div_time.innerHTML = '时间：'+ hours +':'+ minutes +':'+ seconds;
		},1000);
		
		window.onkeydown = function(event){
			if(timer1 != false){
				var div_upper = document.querySelector('.div_upper');
				for(var i=0;i<div_upper.children.length;i++){
					//获取每一个div_upper的子节点的属性key，将其与keyCode对比，判断是否正确
					var key = div_upper.children[i].getAttribute('key');
					var childTop = parseInt(div_upper.children[i].style.top);
					if(childTop > 0 && key == event.keyCode){
						//删除当前的div_upper元素的匹配的子元素
						saveCount++;
						div_upper.removeChild(div_upper.children[i]);
						correct.innerHTML = '正确率:'+ parseInt((1 - count / sum)*100) + '%';
						var speed = document.querySelector('#speed');
						speed.innerHTML = '速度:' + parseInt(60 * saveCount / time ) + '字/分钟';
						break;
					}
				}
			}
		};
	};

	function zanting() {
		clearInterval(timer1);//清除字母下降控制定时器
		clearInterval(timer2);//清除动态产生字母定时器
		clearInterval(timer3);//清除时间计时器
		timer1 = false;
		timer2 = false;
		timer3 = false;
		var countContent = document.querySelector('#count');
		countContent.style.display = 'block';
		if( saveCount < 10){
			countContent.innerHTML = '你拯救了' + saveCount + '个字母，是没油了吗？';
		}else if(saveCount < 20){
			countContent.innerHTML = '你拯救了' + saveCount + '个字母，革命尚未成功啊！';
		}else{
			countContent.innerHTML = '你拯救了' + saveCount + '个字母，感谢你为字母做出的贡献！';
		}
		
	}
	
	function jieshu(){
		zanting();
		var div_upper = document.querySelector('.div_upper');
		var div_time = document.querySelector('#time');
		div_upper.innerHTML = '';
		clearInterval(timer3);
		time = 0;
		div_time.innerHTML = '时间：00:00:00';
		var correct = document.querySelector('#correct');
		correct.innerHTML = '正确率:100%';
		var correct = document.querySelector('#speed');
		correct.innerHTML = '正确率:0 字/分钟';
		
	}

	function tuichu() {
		window.close();
	}
	
	var oBtn1 = document.querySelector('#btn1');
	var oBtn2 = document.querySelector('#btn2');
	var oBtn3 = document.querySelector('#btn3');
	var oBtn4 = document.querySelector('#btn4');
	//开始
	oBtn1.onclick = function() {
		kaishi();
	};
	//暂停
	oBtn2.onclick = function() {
		zanting();
	};
	//结束
	oBtn3.onclick = function() {
		jieshu();
	};
	//退出
	oBtn4.onclick = function() {
		tuichu();
	};
};